/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package job.manager.security.mb;


import com.icesoft.faces.component.menubar.MenuItem;
import java.util.ArrayList;
import java.util.List;
import org.springframework.security.ui.AbstractProcessingFilter;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;

public class LoginMB {

    // properties
    private String userId;

    private String password;

    /**
     * default empty constructor
     */
    public LoginMB() {

        Exception ex = (Exception) FacesContext.getCurrentInstance().getExternalContext()
                .getSessionMap()
                .get(AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY);

        if (ex != null)
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex
                            .getMessage(), ex.getMessage()));

    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public void login(ActionEvent e) throws java.io.IOException {
        HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        FacesContext.getCurrentInstance().getExternalContext().redirect( req.getContextPath() + "/j_spring_security_check?j_username=" + userId + "&j_password=" + password);
    }

    public void logout(ActionEvent e) throws java.io.IOException {
        HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        FacesContext.getCurrentInstance().getExternalContext().redirect( req.getContextPath() + "/j_spring_security_logout");
    }

    public List<MenuItem> getMenyItems()
    {
        //parse the xml schema and produse the meny from it

        return new ArrayList<MenuItem>();
    }
}